package com.Aug.controller.user;

import com.Aug.constant.Constant;
import com.Aug.entity.Dept;
import com.Aug.entity.User;
import com.Aug.exception.UserIsExistException;
import com.Aug.service.DeptService;
import com.Aug.service.UserService;
import com.Aug.util.DataUtil;
import com.Aug.util.IOUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.MultipartConfig;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.Part;
import jdk.nashorn.internal.ir.LiteralNode;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

@WebServlet("/user/update")
@MultipartConfig
public class UserUpdateServlet extends HttpServlet {

    UserService userService = new UserService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        //查询用户
        User user = userService.findUserById(Integer.parseInt(id));

        DeptService deptService = new DeptService();
        List<Dept> depts = deptService.getAllDept();

        //转发user.jsp
        req.setAttribute("user",user);
        req.setAttribute("depts",depts);
        req.getRequestDispatcher("/pages/user/update.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、获取数据
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String deptId = req.getParameter("deptId");
        String id = req.getParameter("id");

        //获取文件部分
        Part profile = null;
        InputStream inputStream = null;
        try {
            profile = req.getPart("profile");
            inputStream = profile.getInputStream();
        }catch(IOException e)
        {
            e.printStackTrace();
        }

        if(profile == null || inputStream ==null){
            throw new RuntimeException("您必须上传头像");
        }

        //需要使用uuid进行处理
        String fileName = UUID.randomUUID() + "_" + profile.getSubmittedFileName();
        //给文件创建目录
        //1、日期的方案
        String format = DataUtil.getNowString();

        String path = Constant.BASE_PATH + format ;
        //若路径不存在，就创建
        File file = new File(path);
        if(!file.exists()) {
            file.mkdirs();
        }

        IOUtil.copy(inputStream,path + "/" + fileName);
        String profileUrl = Constant.BASE_URL_PATH+format+"/"+fileName;

        //封装一个完整用户
        User user = new User(Integer.parseInt(id),username,password,profileUrl);
        Dept dept = new Dept();
        dept.setId(Integer.parseInt(deptId));
        user.setDept(dept);

        try {
            userService.update(user);
            resp.sendRedirect(req.getContextPath() + "/pages/success.jsp");
        }catch (Exception e){
            e.printStackTrace();
            try {
                resp.sendRedirect(req.getContextPath() + "/pages/error.jsp");
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }



    }
}
